<style>
  .g_img{
    margin: 10px 0px;
    width: 120px;
    height: 120px;
  }
</style>
<div style="padding: 15px;" class="layui-form" action="">
 <div class="layui-form-item">

    <label class="layui-form-label">商品名称:</label>
    <div class="layui-inline" style="width:170px;">
     <input type="text" class="layui-input"  lay-verify="required"  name="name" placeholder="商品名称" /> 
    
    </div>
     
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label" >分类:</label>
  

   <div class="layui-inline" style="width:150px;">
    <select name="category_id" >
      {:resolveTree($CateGory)}
    </select>
  </div>
 	

  </div>
    <div class="layui-form-item">
    <label class="layui-form-label" >商品图片:</label>
  

   <div class="layui-inline"  style="width:150px;">
   <button type="button" lay-verify="img" class="layui-btn" id="upload_img"><i class="layui-icon"></i>上传文件</button>
   <img src="" id="img" />
   <input type="hidden" name="logo" value=""/>
  </div>

  </div>
    <div class="layui-form-item">
    <label class="layui-form-label" >商品描述:</label>
    <div class="layui-inline"  >
     <textarea name="title" lay-verify='content' placeholder="不少于10个字" class="layui-textarea"></textarea>
    </div>

  </div>
  <div class="layui-form-item">
    <label class="layui-form-label" >库存:</label>
    <div class="layui-inline" style="width:100px;">
     <input type="text" class="layui-input"  lay-verify="good_num"  name="good_num" value=999 /> 
    </div>

  </div>
    <div class="layui-form-item">
    <label class="layui-form-label" >价格:</label>
    <div class="layui-inline" style="width:100px;">
     <input type="text" class="layui-input"  lay-verify="good_num"  name="price" value=100 /> 
    </div>

  </div>

<div class="layui-form-item">

 <button lay-submit class="layui-btn layui-btn-sm layui-input-block" id="sub">提交</button>
</div>
</div>

<script>

layui.use('form', function(){
  form = layui.form
  ,layer = layui.layer;
  form.verify({
    content:function(value){
      if(value.length<10){
        return '商品描述不少于10个字';
      }
    },
    good_num:function(value){
      if(!(/^[0-9]*$/.test(value))){
        return '库存为正整数';
      }
    },
    img:function(value){
      if($("#img").attr('src').length == 0){
        return '图片没上传';
      }
    }
  })
 
       form.on('submit()', function(data){
          $.post('/admin/good/add',data.field,function(res){
            console.log(res);
             if(res.length != 0)
               layer.msg('错误');
             else
             location.href = '/admin/good';
          })
      
    });
});

//feels like nobody ever loved me until you loved me
layui.use('upload', function(){
 
  upload = layui.upload;
  
  //普通图片上传
  uploadInst = upload.render({
    elem: '#upload_img'
    ,url: '/upload_img'
    ,data:{'type':'goods/'}
     // ,auto:false
     // ,bindAction:'#sub'
    ,choose: function(obj){
       //预读本地文件示例，不支持ie8
      obj.preview(function(index, file, result){
        $('#img').attr('src',result);
        $('#img').addClass('g_img');
      });
    }
    ,acceptMime: 'image/*'
    ,done: function(res, index, upload){
      // //如果上传失败
      console.log(res)
      if(!res){
        return layer.msg('上传失败');
      }
      $('input[name="logo"]').val(res.img_path); 
      

    }
    ,error: function(){
      //演示失败状态，并实现重传
      var demoText = $('#demoText');
      demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
      demoText.find('.demo-reload').on('click', function(){
        uploadInst.upload();
      });
    }
  });
})

   




</script>